from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.base import TaskResult
from autogen_agentchat.conditions import TextMentionTermination, ExternalTermination, MaxMessageTermination, \
    TextMessageTermination
from autogen_agentchat.messages import TextMessage
from autogen_agentchat.teams import RoundRobinGroupChat
from autogen_agentchat.ui import Console
from autogen_core import CancellationToken
from autogen_core.memory import ListMemory, MemoryContent
from autogen_core.model_context import BufferedChatCompletionContext
from autogen_ext.models.openai import OpenAIChatCompletionClient

model_client = OpenAIChatCompletionClient(model="modelscope.cn/Qwen/Qwen2.5-7B-Instruct-GGUF:q5_k_m",
                                          model_info={
                                              "vision": False,
                                              "function_calling": True,
                                              "family": "Qwen3",
                                              "structured_output": True,
                                              "json_output": True,
                                              "multiple_system_messages": True,
                                          },
                                          api_key="ollama",
                                          base_url="http://127.0.0.1:11434/v1")

async def set_context():
    # 创建一个使用 increment_number 函数的工具代理。

    # BufferedChatCompletionContext设置为2表示每次对话最多保留2条消息等于没有上下文
    model_context = BufferedChatCompletionContext(buffer_size=2)
    agent = AssistantAgent(
        "looped_assistant",
        model_client=model_client,
        system_message="你是一个私人助手，用中文回答问题。",
        model_context=model_context
    )
    # 当代理以文本消息响应时停止任务的终止条件。
    result = await agent.run(task="Name two cities in North America.")
    print(result.messages[-1].content)  # type: ignore

    result = await agent.run(task="My favorite color is blue.")
    print(result.messages[-1].content)  # type: ignore

    result = await agent.run(task="Did I ask you any question?")
    print(result.messages[-1].content)  # type: ignore
    print("*"*50)
    print(await model_context.get_messages())
async def no_context():
    # 如果不绑定上下文，那么agent会创建一个默认没有边界的上下文
    agent = AssistantAgent(
        "looped_assistant",
        model_client=model_client,
        system_message="你是一个私人助手，用中文回答问题。",
    )
    # 当代理以文本消息响应时停止任务的终止条件。
    result = await agent.run(task="Name two cities in North America.")
    print(result.messages[-1].content)  # type: ignore

    result = await agent.run(task="My favorite color is blue.")
    print(result.messages[-1].content)  # type: ignore

    result = await agent.run(task="Did I ask you any question?")
    print(result.messages[-1].content)  # type: ignore
    print("*"*50)
    context =agent.model_context
    print(await context.get_messages())

if __name__ == "__main__":
    import asyncio
    asyncio.run(set_context())
    asyncio.run(no_context())